package net.csdn.business.util;

import org.springframework.util.CollectionUtils;

import java.util.List;

import java.util.ArrayList;

public class ListDivider {
    public static <E> List<List<E>> divideList(List<E> list, int numGroups) {
        List<List<E>> dividedList = new ArrayList<>();
        int listSize = list.size();
        if(listSize<numGroups){
            numGroups = listSize;
        }

        int groupSize = (int) Math.ceil((double) listSize / numGroups);

        for (int i = 0; i < listSize; i += groupSize) {
            dividedList.add(new ArrayList<>(list.subList(i, Math.min(i + groupSize, listSize))));
        }

        return dividedList;
    }




    public static <E> List<List<E>> divideListWithGroupSize(List<E> list, int groupSize) {
        List<List<E>> dividedList = new ArrayList<>();
        int listSize = list.size();

        for (int i = 0; i < listSize; i += groupSize) {
            dividedList.add(new ArrayList<>(list.subList(i, Math.min(i + groupSize, listSize))));
        }

        return dividedList;
    }


    public static <E> List<E> getDifference(List<E> list1, List<E> list2) {
        List<E> difference = new ArrayList<>(list1);
        if(!CollectionUtils.isEmpty(list2)){
            difference.removeAll(list2);
        }
        return difference;
    }

    public static void main(String[] args) {
        List a = new ArrayList();;;
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(4);
        a.add(5);
        List list = divideListWithGroupSize(a, 2);
        System.out.println(list);
    }

}
